package Servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Service.PaperService;

import pojo.PageBean;
import pojo.Paper;


/**
 * Servlet implementation class PaperServlet
 */
@WebServlet("/PaperServlet")
public class PaperServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PaperServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String method=request.getParameter("method");
		System.out.println("1111"+method);
		switch(method) {
		case "showAll":
			showAll(request,response);
			break;
		case "add":
			add(request,response);
			break;
		case "up":
			up(request,response);
			break;
		case "update":
			update(request,response);
			break;
		}
	}
		private void showAll(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
			request.setCharacterEncoding("utf-8");
	    	int currentPage = Integer.parseInt(request.getParameter("currentPage"));
	    	int pageSize=5;
	    	PaperService ps = new PaperService();
	    	PageBean<Paper> pb = ps.show(currentPage, pageSize);
	    	request.setAttribute("pb", pb);
	    	try {
				try {
					request.getRequestDispatcher("1.jsp").forward(request, response);
				} catch (ServletException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
		private void add(HttpServletRequest request, HttpServletResponse response) {
			// TODO Auto-generated method stub
			try {
				request.setCharacterEncoding("utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			Paper p =new Paper();
			p.setTitle(request.getParameter("title"));
			p.setType(request.getParameter("type"));
			p.setPublications(request.getParameter("publications"));
			p.setTime(request.getParameter("time"));
			p.setAuthor(request.getParameter("author"));
			p.setPapertitle(request.getParameter("parpertitle"));
			p.setUnit(request.getParameter("unit"));
			p.setRate(request.getParameter("rate"));
			System.out.println("11111111"+p);
			PaperService pservice = new PaperService();
			int i = pservice.add(p);
			if(i>0) {
				try {
					response.sendRedirect("PaperServlet?method=showAll&currentPage=1");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else {
				try {
					response.sendRedirect("lunwen.jsp");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
				}

		private void up(HttpServletRequest request, HttpServletResponse response) {
			// TODO Auto-generated method stub
			int id=Integer.parseInt(request.getParameter("id"));
			PaperService pservice = new PaperService();
			Paper p = pservice.getPaper(id);
			if(p!=null) {
				HttpSession session=request.getSession(true);
				session.setAttribute("P", p);
				try {
					response.sendRedirect("UpdatePaper.jsp");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		private void update(HttpServletRequest request, HttpServletResponse response) {
			// TODO Auto-generated method stub
			try {
				request.setCharacterEncoding("utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			String title=request.getParameter("title");
			String type=request.getParameter("type");
			String publications=request.getParameter("publications");
			String author = request.getParameter("author");
			String time=request.getParameter("time");
			String papertitle=request.getParameter("papertitle");
			String unit=request.getParameter("unit");
			String rate = request.getParameter("rate");
//			Part part;
//			try {
//				part = request.getPart("p_image");
//				String filename=part.getSubmittedFileName();
//				String str = this.getServletContext().getRealPath("image");
//				File f=new File(str);
//				if (!f.exists()){
//					f.mkdir();
//				}
//			    String name1 = UUID.randomUUID()+filename;
//			    img="image/"+name1;
//				part.write(str+"/"+name1);
//			} catch (IOException e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//				
//			} catch (ServletException e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}
			
			Paper p =new Paper();
			
			p.setTitle(title);
			p.setType(type);
			p.setPublications(publications);
			p.setTime(time);
			p.setAuthor(author);
			p.setPapertitle(papertitle);
			p.setUnit(unit);
			p.setRate(rate);
			PaperService pservice=new PaperService();
			int i = pservice.update(p);
			if(i>0) {
				try {
					response.sendRedirect("http://localhost:8080/kygl/PaperServlet?method=showAll&currentPage=1");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else {
				try {
					response.sendRedirect("UpdatePaper.jsp");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
	}
	}